package com.space.feign;

import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/5/17 2:24 下午
 */
public class FeignFactoryBean<T> implements FactoryBean<T> {
    @Value("${provider.url}")
    private String providerUrl;

    private Class<T> feignInterface;

    public FeignFactoryBean() {
    }

    public FeignFactoryBean(Class<T> feignInterface) {
        this.feignInterface = feignInterface;
    }

    @Override
    public T getObject() throws Exception {
        return Feign.builder()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .options(new Request.Options(1000, 3500))
                .retryer(new Retryer.Default(5000, 5000, 3))
                .target(this.feignInterface, providerUrl);
    }

    @Override
    public Class<?> getObjectType() {
        return this.feignInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
